package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.teaching.model.dto.CompanyDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.teaching.convert.CompanyConvert;
import com.xuecheng.teaching.entity.Company;
import com.xuecheng.teaching.mapper.CompanyMapper;
import com.xuecheng.teaching.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {


    public CompanyDTO getByTenantId(Long tenantId) {
        if (tenantId == null || tenantId <= 0) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getTenantId, tenantId);
        Company company = this.getOne(queryWrapper);
        return CompanyConvert.INSTANCE.entity2dto(company);
    }

    @Override
    public CompanyDTO getCompanyDetail(CompanyDTO company) {
        Long companyId = company.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)){
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        Company companyVo = this.getById(companyId);

        CompanyDTO companyDTO = CompanyConvert.INSTANCE.entity2dto(companyVo);

        if (ObjectUtils.isEmpty(companyDTO)){
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }
        return companyDTO;
    }


    /**
     * 修改 company资料
     * @param companyDTO
     * @return
     */
    @Override
    public Boolean modifyCompany(CompanyDTO companyDTO) {

        if (ObjectUtils.isEmpty(companyDTO)){
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        Company company = CompanyConvert.INSTANCE.dto2entity(companyDTO);

        LambdaUpdateWrapper<Company> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Company::getId,companyDTO.getCompanyId());

        boolean updateResult = this.update(company,updateWrapper);

        return updateResult;
    }
}
